﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class PayFines : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        lblMessageTop.Text = "";
        panelPay.Visible = false;

        //string searchTxt = "%" + txtSearch.Text.Trim() + "%";
        string searchTxt = txtSearch.Text.Trim();

        int libraryId = int.Parse(Request.QueryString["id"]);
        ManagePerson mp = new ManagePerson();
        DataSet ds = mp.findPerson(searchTxt, libraryId);

        GridView_Person.DataSource = ds;
        GridView_Person.DataBind();
    }
    
    protected void btnAmount_Click(object sender, EventArgs e)
    {
        int personId = int.Parse(GridView_Person.DataKeys[0].Value.ToString());
        float amtPaid = 0;
        
        try 
        {
            amtPaid = float.Parse(txtAmount.Text);
        }
        catch (Exception ex)
        {
            lblMessage.Text = "Please Enter a number. " + ex.Message.ToString();
        }

        ManagePatron mp = new ManagePatron();
        float fine = mp.getPatronFines_LibrarianView(personId);

        if (fine <= 0)
            lblMessage.Text = "This patron does not have any fines to pay.";
        else if(amtPaid > fine)
            lblMessage.Text = "You can not pay more than the amount due.";
        else
        {
            if (amtPaid > 0)
            {
                GetFines.Fines getFine = new GetFines.Fines();
                int affectedRows = getFine.GetFines(personId, amtPaid);
                if (affectedRows > 0)
                    lblMessage.Text = "Payment Applied";
                else
                    lblMessage.Text = "There was a problem please try again.";

                getDataForGrid(personId);
            }
            else
            {
                lblMessage.Text = "Please Enter an amount";
            }
        }
    }
    
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int personId = int.Parse(GridView_Person.SelectedDataKey.Value.ToString());

        getDataForGrid(personId);
        //get new dataset for grid based on selected row
        //int libraryId = int.Parse(Request.QueryString["id"]);
        //ManagePerson mp = new ManagePerson();
        //DataSet ds = mp.findPersonById(personId, libraryId);
        //GridView_Person.DataSource = ds;
        //GridView_Person.DataBind();

        ManagePatron mp = new ManagePatron();
        float fine = mp.getPatronFines_LibrarianView(personId);

        if (fine <= 0)
            lblMessageTop.Text = "This patron does not have any fines to pay.";
        else
            panelPay.Visible = true;
    }

    protected void getDataForGrid(int personId)
    {
        int libraryId = int.Parse(Request.QueryString["id"]);
        ManagePerson mp = new ManagePerson();
        DataSet ds = mp.findPersonById(personId, libraryId);
        GridView_Person.DataSource = ds;
        GridView_Person.DataBind();
    }
}
